package com.jt.aop;

import com.jt.anno.Pri;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Vector;

@Aspect
@Component
public class SpringAOP {

    private List<String> list = new Vector<>();


    @PostConstruct
    public void init() {
        list.add("add");
        list.add("update");
    }

    @Around("@annotation(pri)")
    public Object around2(ProceedingJoinPoint joinPoint, Pri pri) throws Throwable {
        String name = pri.name();
        System.out.println("用户的权限" + name);
        if (list.contains(name)) {
            Object result = joinPoint.proceed();
            return result;
        } else {
            System.out.println("没有权限");
        }

        return null;
    }

    //  @Around("execution(* com.jt.service..*.*(..))")
    public Object around1(ProceedingJoinPoint joinPoint) throws Throwable {
        long t1 = System.currentTimeMillis();
        System.out.println("开始");
        Object result = joinPoint.proceed();
        System.out.println("结束");
        Class aClass = joinPoint.getTarget().getClass();
        String className = joinPoint.getSignature().getDeclaringTypeName();
        Class declaringType = joinPoint.getSignature().getDeclaringType();
        String name = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        System.out.println(aClass);
        System.out.println(className);
        System.out.println(declaringType);
        System.out.println(name);
        System.out.println(Arrays.toString(args));
        long t2 = System.currentTimeMillis();
        System.out.println(t2 - t1);

        return result;
    }
}
